package dao;

import javabean.Message;
import util.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MessageDAO {

    // 添加新留言
    public void addMessage(Message msg) throws SQLException {
        String sql = "INSERT INTO messages(name, time, qq, email, content) VALUES(?, ?, ?, ?, ?)";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, msg.getName());
            stmt.setTimestamp(2, new Timestamp(msg.getTime().getTime()));
            stmt.setString(3, msg.getQq());
            stmt.setString(4, msg.getEmail());
            stmt.setString(5, msg.getContent());
            stmt.executeUpdate();
        }
    }

    // 获取所有留言（分页）
    public List<Message> getAllMessages(int page, int pageSize) throws SQLException {
        List<Message> messages = new ArrayList<>();
        String sql = "SELECT * FROM messages ORDER BY time DESC LIMIT ?, ?";

        try (Connection conn = DBUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, (page - 1) * pageSize);
            stmt.setInt(2, pageSize);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Message msg = new Message();
                    msg.setId(rs.getInt("id"));
                    msg.setName(rs.getString("name"));
                    msg.setTime(rs.getTimestamp("time"));
                    msg.setQq(rs.getString("qq"));
                    msg.setEmail(rs.getString("email"));
                    msg.setContent(rs.getString("content"));
                    msg.setReply(rs.getString("reply"));
                    messages.add(msg);
                }
            }
        }
        return messages;
    }

    // 删除留言
    public void deleteMessage(int id) throws SQLException {
        String sql = "DELETE FROM messages WHERE id=?";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    // 添加回复
    public void addReply(int id, String reply) throws SQLException {
        String sql = "UPDATE messages SET reply=? WHERE id=?";
        try (Connection conn = DBUtils.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, reply);
            stmt.setInt(2, id);
            stmt.executeUpdate();
        }
    }

    // 获取总留言数（用于分页）
    public int getTotalMessages() throws SQLException {
        String sql = "SELECT COUNT(*) FROM messages";
        try (Connection conn = DBUtils.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            if (rs.next()) {
                return rs.getInt(1);
            }
        }
        return 0;
    }

    public List<Message> getMessages(int page, int pageSize) {
        return null;
    }
}